package at.jku.pervasive.gui.action;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;

import at.jku.pervasive.Application;
import at.jku.pervasive.Measurement;
import at.jku.pervasive.gui.jformdesigner.UbisenseApplicationFrame;

public class LoadMeasurementAction extends AbstractAction
{

	private final JFileChooser loadMeasurementFileChooser;
	private final UbisenseApplicationFrame appFrame;

	public LoadMeasurementAction(UbisenseApplicationFrame appFrame)
	{
		super();

		putValue(NAME, "Load measurement ...");

		this.appFrame = appFrame;

		File openFolder = Application.getInstance().getDefaultOpenMeasurementFolder();
		loadMeasurementFileChooser = new JFileChooser(openFolder);
		loadMeasurementFileChooser.setMultiSelectionEnabled(false);
		loadMeasurementFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	}

	public void actionPerformed(ActionEvent e)
	{
		// open the file chooser
		int chosenOption = loadMeasurementFileChooser.showOpenDialog(null);
		if (chosenOption == JFileChooser.APPROVE_OPTION)
		{
			File measurementFile = loadMeasurementFileChooser.getSelectedFile();
			try
			{
				Measurement measurement = Application.getInstance().loadMeasurement(measurementFile);
				this.appFrame.setLoadedMeasurement(measurement);
			} catch (FileNotFoundException e1)
			{
				this.appFrame.setStatusText(e1.getMessage());
			}
		}
	}

}
